package com.xhd.config;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.xhd.action.Action;
import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.base.obj.BaseObject;
import com.xhd.base.obj.EnpObjFactory;
import com.xhd.base.scheduler.AsyncProcessMgr;
import com.xhd.base.scheduler.SchedulerQueue;
import com.xhd.base.util.xBaseObj;
import com.xhd.db.DaoTransaction;
import com.xhd.exception.IllegalDataException;
import com.xhd.exception.SystemException;
import com.xhd.exception.XException;
import com.xhd.server.ProcessHandle;
import com.xhd.server.ProcessHandleMgr;
import com.xhd.servlet.ClientAction;

public class ActionControl {
	 private static ActionControl jm16 = null;
	  private static final Log jm9;
	  private EnpObjFactory jm482 = null;
	  static Class jm10;

	  static
	  {
	    Class tmp7_4 = jm10;
	    if (tmp7_4 == null)
	    {
	    
	      try
	      {
	        ActionControl.jm10 = Class.forName("com.sinocc.base.server.ActionControl");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp7_4);
	  }

	  public static synchronized ActionControl getInstance()
	  {
	    if (jm16 == null)
	      jm16 = new ActionControl();
	    jm16.jm482 = EnpObjFactory.getInstance();
	    return jm16;
	  }

	  public xBaseObj getBizObj(Action paramAction)
	    throws XException
	  {
	    String str = paramAction.getServiceName();
	    BizObjConfig localEnpObjConfig = BizObjConfig.getObjConfig(str);
	    Class localClass = null;
	    Object localObject;
	    if (localEnpObjConfig != null)
	    {
	      localObject = localEnpObjConfig.getJavaName();
	      if ((localObject != null) && (((String)localObject).length() > 0))
	        try
	        {
	          localClass = Class.forName((String)localObject);
	        }
	        catch (Exception localException)
	        {
	          jm9.error(localException);
	          throw new IllegalDataException(50003, (String)localObject);
	        }
	    }
	    if (localClass == null)
	      localClass = this.jm482.getBizClass(str);
	    if (localClass == null)
	      throw new IllegalDataException(50003, str);
	    try
	    {
	      localObject = (xBaseObj)localClass.newInstance();
	      localClass = ((xBaseObj)localObject).findForwardClass(paramAction);
	      if (localClass != null)
	        localObject = (xBaseObj)localClass.newInstance();
	      return (xBaseObj )localObject;
	    }
	    catch (Throwable localThrowable)
	    {
	      jm9.error(localThrowable);
	      throw new IllegalDataException(50003, str);
	    }
	  }

	  public Throwable processTransaction(xBaseObj paramxBaseObj, Action paramAction)
	  {
	    return processTransaction(paramxBaseObj, paramAction, ProcessHandleMgr.getHandle());
	  }

	  public Throwable processTransaction(xBaseObj paramxBaseObj, Action paramAction, ProcessHandle paramProcessHandle)
	  {
	    boolean bool = false;
	    Object localObject1 = null;
	    DaoTransaction localDaoTransaction = null;
	    Object localObject2;
	    Object localObject5;
	    try
	    {
	      if (paramProcessHandle == null)
	        throw new SystemException("No Process Handle.");
	      if (paramProcessHandle.getBizObj() == null)
	        paramProcessHandle.setBizObj(paramxBaseObj);
	      if (!(paramxBaseObj.isEntrance()))
	        paramxBaseObj.setEntrance();
	      ClientAction localClientAction = paramProcessHandle.getAction();
	      if (paramxBaseObj.getClientAction() == null)
	        paramxBaseObj.setClientAction(localClientAction);
	      localClientAction.setActionTime(System.currentTimeMillis());
	      if (localClientAction.getMethodName() == null)
	        localClientAction.setMethodName(paramAction.getMethodName());
	      localObject2 = ActionConfig.getConfig(paramAction);
	      bool = ((ActionConfig)localObject2).isLog();
	      localClientAction.setLogProcess(bool);
	      localDaoTransaction = paramxBaseObj.getDaoTransaction();
	      localDaoTransaction.setTransation(((ActionConfig)localObject2).isDBTran());
	      Method localMethod1 = null;
	      int i = 0;
	      BizObjClass localBizObjClass = BizObjClass.getInstance(paramxBaseObj);
	      localMethod1 = localBizObjClass.getActionsMethod();
	      if (localMethod1 != null)
	        i = 1;
	      else
	        localMethod1 = localBizObjClass.getActionMethod(paramAction.getMethodName());
	      if (localMethod1 == null)
	        throw new IllegalDataException(50002, paramAction.getMethodName());
	      if (i != 0)
	        ((BaseObject)paramxBaseObj).setXmlTool(NullXmlTool.getInstance());
	      Method localMethod2 = localBizObjClass.getEventMethod("beforeinvoke");
	      if (localMethod2 != null)
	        localMethod2.invoke(paramxBaseObj, null);
	      if (i == 0)
	      {
	        localMethod1.invoke(paramxBaseObj, null);
	      }
	      else
	      {
	        Object[] arrayOfObject = new Object[1];
	        arrayOfObject[0] = paramAction.getMethodName();
	        localMethod1.invoke(paramxBaseObj, arrayOfObject);
	      }
	      if (paramxBaseObj.getThrowable() != null)
	        throw paramxBaseObj.getThrowable();
	      localMethod2 = localBizObjClass.getEventMethod("afterinvoke");
	      if (localMethod2 != null)
	        localMethod2.invoke(paramxBaseObj, null);
	      localDaoTransaction.commit();
	      return null;
	    }
	    catch (InvocationTargetException localInvocationTargetException)
	    {
	      if (localDaoTransaction != null)
	        localDaoTransaction.rollback();
	      localObject2 = localInvocationTargetException.getTargetException();
	      if (localObject2 == null)
	        localObject2 = localInvocationTargetException;
	      jm9.error(paramAction.getFullName(), (Throwable)localObject2);
	      localObject1 = localObject2;
	      localObject5 = localObject2;
	      return (Throwable) localObject5;
	    }
	    catch (Throwable localThrowable)
	    {
	      if (localDaoTransaction != null)
	        localDaoTransaction.rollback();
	      localObject1 = localThrowable;
	      localObject5 = localThrowable;
	      return (Throwable) localObject5;
	    }
	    finally
	    {
	      if (localDaoTransaction != null)
	        localDaoTransaction.close();
	      jm481(paramxBaseObj, bool, localObject1);
	      paramxBaseObj = null;
	    }
	  }

	  protected Throwable schedulerTransaction(xBaseObj paramxBaseObj, ProcessHandle paramProcessHandle)
	  {
	    DaoTransaction localDaoTransaction = null;
	    try
	    {
	      if ((paramProcessHandle == null) || (paramxBaseObj.getClientAction() == null))
	        throw new SystemException("No Process Handle.");
	      if (paramProcessHandle.getBizObj() == null)
	        paramProcessHandle.setBizObj(paramxBaseObj);
	      if (!(paramxBaseObj.isEntrance()))
	        paramxBaseObj.setEntrance();
	      localDaoTransaction = paramxBaseObj.getDaoTransaction();
	      localDaoTransaction.setTransation(true);
	      try
	      {
	        SchedulerQueue.getInstance().pushTask(paramxBaseObj, paramxBaseObj.getClientAction());
	      }
	      catch (Exception localException)
	      {
	        jm9.error("Scheduler", localException);
	        throw localException;
	      }
	      localDaoTransaction.commit();
	      return null;
	    }
	    catch (Throwable localThrowable1)
	    {
	      if (localDaoTransaction != null)
	        localDaoTransaction.rollback();
	      Throwable localThrowable2 = localThrowable1;
	      return localThrowable2;
	    }
	    finally
	    {
	      if (localDaoTransaction != null)
	        localDaoTransaction.close();
	      paramxBaseObj = null;
	    }
	  }

	  protected Throwable asyncTransaction(xBaseObj paramxBaseObj, ProcessHandle paramProcessHandle)
	  {
	    try
	    {
	      if ((paramProcessHandle == null) || (paramxBaseObj.getClientAction() == null))
	        throw new SystemException("No Process Handle.");
	      if (paramProcessHandle.getBizObj() == null)
	        paramProcessHandle.setBizObj(paramxBaseObj);
	      if (!(paramxBaseObj.isEntrance()))
	        paramxBaseObj.setEntrance();
	      try
	      {
	        AsyncProcessMgr.getInstance().requestProcess(paramxBaseObj);
	      }
	      catch (Exception localException)
	      {
	        jm9.error("asyncTransaction", localException);
	        throw localException;
	      }
	      return null;
	    }
	    catch (Exception localThrowable)
	    {
	    }
	    return localThrowable;
	  }

	  private void jm481(xBaseObj paramxBaseObj, boolean paramBoolean, Throwable paramThrowable)
	  {
	    if ((paramThrowable == null) && (!(paramBoolean)))
	      return;
	    jm9.process(paramxBaseObj, paramThrowable);
	    paramxBaseObj = null;
	  }
}
